package com.erp.module.dingDan.sSDingDan

import android.app.Activity
import android.content.Intent
import android.view.KeyEvent
import android.view.View
import android.widget.ArrayAdapter
import com.erp.activity.BaseActivity
import com.erp.activity.MainKActivity
import com.erp.activity.SeekActivity
import com.erp.bean.OrderSousuoBean
import com.erp.bean.Twostringbean
import com.erp.popupwin.PopShejiXiaoshouDuoxuan
import com.erp.popupwin.PopShejiXiaoshouDuoxuan.Companion.TF
import com.erp.R
import com.erp.util.Dialogutil
import com.erp.util.Myutil
import com.erp.util.Sputil
import com.erp.module.dingDan.FragmentDingDan
import kotlinx.android.synthetic.main.activity_prodyction.*
import kotlinx.android.synthetic.main.header_knoya.*
import java.util.*
import kotlin.collections.ArrayList

/**
 * Created by Administrator on 2016/11/16/016.
 */
class ProdyctionActivity : BaseActivity(), View.OnClickListener {
    private val shengfen = arrayOf("", "河北", "辽宁", "北京", "黑龙江", "吉林", "香港", "澳门", "台湾", "陕西", "甘肃", "宁夏", "青海", "新疆", "重庆", "四川", "贵州", "云南", "西藏", "海南", "广西", "广东", "天津", "山西", "内蒙古", "湖南", "河南", "湖北", "江苏", "浙江", "上海", "山东", "山西", "福建", "安徽", "海外")
    lateinit var sousuoBean: OrderSousuoBean
    lateinit var mtefu : MutableList<String>
    override fun getLayoutId(): Int {
        activityname = a
        return R.layout.activity_prodyction
    }

    override fun initCreate() {
        initView()
        initData()
        initClick()
        et_suoyou_suosuo.isFocusable = true
        et_suoyou_suosuo.isFocusableInTouchMode = true
        et_suoyou_suosuo.requestFocus()
        et_suoyou_suosuo.requestFocusFromTouch()
    }

    //控件监听
    private fun initClick() {
        but_queding.setOnClickListener(this)
        tv_start_time.setOnClickListener(this)
        tv_end_time.setOnClickListener(this)
        iv_header_left.setOnClickListener(this)
    }

    fun getsearchparams() {
        sousuoBean.all = et_suoyou_suosuo.text.toString()
        sousuoBean.onum = tv_diangdanhao.text.toString()
        sousuoBean.o_contract_num_like = tv_hetonghao.text.toString()
        sousuoBean.o_customer_like = tv_kehuming.text.toString()
        sousuoBean.province = sp_shengfen.selectedItem.toString()
        sousuoBean.shop_name = tv_zhuanmaidian.text.toString()
        getquyu()//获得区域参数
        getleixing()//获得单类型参数
        getzhaungtai()//获得书否取消状态参数
        getisnull()//获得属于空的参数
        getisnonull()//获得非空的参数
    }

    private fun getquyu() {
        if (sp_quyu.selectedItem.toString() !== "") {
            when (sp_quyu.selectedItem.toString()) {
                "东北区" -> {
                    sousuoBean.area = "1"
                    sousuoBean.areas = "东北区"
                }
                "华东一区" -> {
                    sousuoBean.area = "5"
                    sousuoBean.areas = "华东一区"
                }
                "华东三区" -> {
                    sousuoBean.area = "7"
                    sousuoBean.areas = "华东三区"
                }
                "华东二区" -> {
                    sousuoBean.area = "6"
                    sousuoBean.areas = "华东二区"
                }
                "华南区" -> {
                    sousuoBean.area = "8"
                    sousuoBean.areas = "华南区"
                }
                "华总二区" -> {
                    sousuoBean.area = "4"
                    sousuoBean.areas = "华总二区"
                }
                "华中一区" -> {
                    sousuoBean.area = "3"
                    sousuoBean.areas = "华中一区"
                }
                "华北区" -> {
                    sousuoBean.area = "2"
                    sousuoBean.areas = "华北区"
                }
                "直营区" -> {
                    sousuoBean.area = "11"
                    sousuoBean.areas = "直营区"
                }
                "西北区" -> {
                    sousuoBean.area = "9"
                    sousuoBean.areas = "西北区"
                }
                "西南区" -> {
                    sousuoBean.area = "10"
                    sousuoBean.areas = "西南区"
                }
            }
        }
    }

    //获得书否取消状态参数
    private fun getzhaungtai() {
        if (sp_zhaungtai.selectedItem.toString() !== "") {
            val xsy = Twostringbean()
            when (sp_zhaungtai.selectedItem.toString()) {
                "是" -> sousuoBean.is_canceled = "yes"
                "否" -> sousuoBean.is_canceled = "no"
            }
        }
    }

    //获得非空的参数
    private fun getisnonull() {
        if (sp_ifnonull!!.selectedItem.toString() !== "") {
            val xsy = Twostringbean()
            when (sp_ifnonull!!.selectedItem.toString()) {
                "下单受理时间" -> {
                    sousuoBean.field_not_empty = "o.cd_time"
                    sousuoBean.field_not_emptys = "下单受理时间"
                }
                "报价时间" -> {
                    sousuoBean.field_not_empty = "o.quoted_price_time"
                    sousuoBean.field_not_emptys = "报价时间"
                }
                "定包时间" -> {
                    sousuoBean.field_not_empty = "o.dingbao_time"
                    sousuoBean.field_not_emptys = "定包时间"
                }
                "收款时间" -> {
                    sousuoBean.field_not_empty = "o.charge_audit_time"
                    sousuoBean.field_not_emptys = "收款时间"
                }
                "入仓时间" -> {
                    sousuoBean.field_not_empty = "o.warehousing_time"
                    sousuoBean.field_not_emptys = "入仓时间"
                }
                "发货时间" -> {
                    sousuoBean.field_not_empty = "o.deliver_time"
                    sousuoBean.field_not_emptys = "发货时间"
                }
            }
        }
    }

    //获得属于空的参数
    private fun getisnull() {
        if (sp_ifnull.selectedItem.toString() != "") {
            when (sp_ifnull.selectedItem.toString()) {
                "下单受理时间" -> {
                    sousuoBean.field_empty = "o.cd_time"
                    sousuoBean.field_emptys = "下单受理时间"
                }
                "报价时间" -> {
                    sousuoBean.field_empty = "o.quoted_price_time"
                    sousuoBean.field_emptys = "报价时间"
                }
                "定包时间" -> {
                    sousuoBean.field_empty = "o.dingbao_time"
                    sousuoBean.field_emptys = "定包时间"
                }
                "收款时间" -> {
                    sousuoBean.field_empty = "o.charge_audit_time"
                    sousuoBean.field_emptys = "收款时间"
                }
                "入仓时间" -> {
                    sousuoBean.field_empty = "o.warehousing_time"
                    sousuoBean.field_emptys = "入仓时间"
                }
                "发货时间" -> {
                    sousuoBean.field_empty = "o.deliver_time"
                    sousuoBean.field_emptys = "发货时间"
                }
            }
        }
    }

    //获得单类型参数
    private fun getleixing() {
        if (sp_leixing.selectedItem.toString() !== "") {
            val xsy = Twostringbean()
            when (sp_leixing.selectedItem.toString()) {
                "A单" -> {
                    sousuoBean.o_type = "12"
                    sousuoBean.o_types = "A单"
                }
                "B单" -> {
                    sousuoBean.o_type = "9"
                    sousuoBean.o_types = "B单"
                }
                "C单" -> {
                    sousuoBean.o_type = "15"
                    sousuoBean.o_types = "C单"
                }
                "D单" -> {
                    sousuoBean.o_type = "17"
                    sousuoBean.o_types = "D单"
                }
                "E单" -> {
                    sousuoBean.o_type = "16"
                    sousuoBean.o_types = "E单"
                }
                "MA单" -> {
                    sousuoBean.o_type = "25"
                    sousuoBean.o_types = "MA单"
                }
                "MB单" -> {
                    sousuoBean.o_type = "18"
                    sousuoBean.o_types = "MB单"
                }
                "直发" -> {
                    sousuoBean.o_type = "10"
                    sousuoBean.o_types = "直发"
                }
                "J单" -> {
                    sousuoBean.o_type = "20,21,22,26,20"
                    sousuoBean.o_types = "J单"
                }
                "--JF单" -> {
                    sousuoBean.o_type = "20"
                    sousuoBean.o_types = "--JF单"
                }
                "--JH单" -> {
                    sousuoBean.o_type = "21"
                    sousuoBean.o_types = "--JH单"
                }
                "--JK单" -> {
                    sousuoBean.o_type = "22"
                    sousuoBean.o_types = "--JK单"
                }
                "--JN单" -> {
                    sousuoBean.o_type = "26"
                    sousuoBean.o_types = "--JN单"
                }
                "AE单" -> {
                    sousuoBean.o_type = "23"
                    sousuoBean.o_types = "AE单"
                }
            }

        }
    }

    //获得需要搜索的时间参数
    fun gettime() {
        when (sp_istime.selectedItem.toString()) {
            "订单创建时间" -> {
                sousuoBean.time_category = "o.ctime"
                sousuoBean.time_categorys = "订单创建时间"
            }
            "下单申请时间" -> {
                sousuoBean.time_category = "o.sqxd_time"
                sousuoBean.time_categorys = "订单创建时间"
            }
            "下单受理时间" -> {
                sousuoBean.time_category = "o.cd_time"
                sousuoBean.time_categorys = "订单创建时间"
            }
            "报价时间" -> {
                sousuoBean.time_category = "o.quoted_price_time"
                sousuoBean.time_categorys = "订单创建时间"
            }
            "收款时间" -> {
                sousuoBean.time_category = "o.charge_audit_time"
                sousuoBean.time_categorys = "订单创建时间"
            }
            "入仓时间" -> {
                sousuoBean.time_category = "o.warehousing_time"
                sousuoBean.time_categorys = "订单创建时间"
            }
            "发货时间" -> {
                sousuoBean.time_category = "o.deliver_time"
                sousuoBean.time_categorys = "订单创建时间"
            }
        }
        if (tv_start_time.text.toString() != "") {
            sousuoBean.start_time = tv_start_time!!.text.toString()
        }
        if (tv_end_time.text.toString() != "") {
            sousuoBean.end_time = tv_end_time!!.text.toString()
        }
    }

    //加载数据
    private fun initData() {
        val shengfens = ArrayList(Arrays.asList(*shengfen))
        val shengfenadapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, shengfens)
        sp_shengfen.adapter = shengfenadapter
        et_suoyou_suosuo.setText(sousuoBean.all)
        tv_diangdanhao.text = sousuoBean.onum
        tv_hetonghao.text = sousuoBean.o_contract_num_like
        tv_kehuming.text = sousuoBean.o_customer_like
        tv_zhuanmaidian.text = sousuoBean.shop_name
        for (j in shengfen.indices) {
            if (shengfen[j] == sousuoBean.province) {
                sp_shengfen.setSelection(j)
                break
            }
        }
        val quyu = arrayOf("1", "5", "7", "6", "8", "4", "3", "2", "11", "9", "10")
        for (j in quyu.indices) {
            if (quyu[j] == sousuoBean.area) {
                sp_quyu.setSelection(j + 1)
                break
            }
        }
        val quy = arrayOf("12", "9", "15","17","16","25","18", "10","20,21,22,26,20","20","21","22","26","23")
        for (j in quy.indices) {
            if (quy[j] == sousuoBean.o_type) {
                sp_leixing!!.setSelection(j + 1)
                break
            }
        }
        val quyu1 = arrayOf("yes", "no")
        for (j in quyu1.indices) {
            if (quyu1[j] == sousuoBean.is_canceled) {
                sp_zhaungtai.setSelection(j + 1)
                break
            }
        }
        val quyu2 = arrayOf("o.cd_time", "o.quoted_price_time", "o.dingbao_time", "o.charge_audit_time", "o.warehousing_time", "o.deliver_time")
        for (j in quyu2.indices) {
            if (quyu2[j] == sousuoBean.field_empty) {
                sp_ifnull.setSelection(j + 1)
                break
            }
        }
        val quyu3 = arrayOf("o.cd_time", "o.quoted_price_time", "o.dingbao_time", "o.charge_audit_time", "o.warehousing_time", "o.deliver_time")
        for (j in quyu3.indices) {
            if (quyu3[j] == sousuoBean.field_not_empty) {
                sp_ifnonull.setSelection(j + 1)
                break
            }
        }
        if (sousuoBean.time_category != "") {
            val quyu4 = arrayOf("o.ctime", "o.sqxd_time", "o.cd_time", "o.quoted_price_time", "o.charge_audit_time", "o.warehousing_time", "o.deliver_time")
            for (i in quyu4.indices) {
                if (quyu4[i] == sousuoBean.time_category) {
                    sp_istime.setSelection(i)
                    break
                }
            }
            tv_start_time.text = sousuoBean.start_time
            tv_end_time.text = sousuoBean.end_time
        }
        mtefu=sousuoBean.special_service_cat
        if(mtefu.size>0) {
            val shejishi = StringBuffer("")
            for (j in 0 until mtefu.size) {
                if (j == mtefu.size - 1) {
                    shejishi.append(mtefu[j])
                } else {
                    shejishi.append(mtefu[j])
                    shejishi.append(" ")
                }
            }
            Myutil.settv(shejishi.toString(), tvTeFu)
        }
    }

    //初始化控件
    private fun initView() {
        mtefu=ArrayList()
        sousuoBean = OrderSousuoBean()
        intent = Intent()
        editor = Sputil.editor("query")
        sp = Sputil.sp("query")
        tv_kehuming.setOnClickListener(this)
        tv_hetonghao.setOnClickListener(this)
        tv_diangdanhao.setOnClickListener(this)
        tv_zhuanmaidian.setOnClickListener(this)
        tvTeFu.setOnClickListener(this)
        iv_header_left.visibility = View.VISIBLE
        tv_header_title.text = "生产订单查询"
        sousuoBean = FragmentDingDan.sousuoBean
        if(MainKActivity.ZhuanMaiDianid =="0"){
            LlTeFu.visibility=View.VISIBLE
        }else{
            LlTeFu.visibility=View.GONE
        }
    }


    override fun onClick(view: View) {
        when (view) {
            tv_diangdanhao -> startSeek(SeekActivity.SEEK_ORDER, 7, "生产单号", tv_diangdanhao.text.toString())
            tv_hetonghao -> startSeek(SeekActivity.SEEK_HETONG, 8, "合同号", tv_hetonghao.text.toString())
            tv_kehuming -> startSeek(SeekActivity.SEEK_KEHUMING, 9, "客户名", tv_kehuming.text.toString())
            tv_zhuanmaidian -> startSeek(SeekActivity.SEEK_ZMD, 10, "专卖店", tv_zhuanmaidian.text.toString())
            tvTeFu -> {
                val pop = PopShejiXiaoshouDuoxuan(this@ProdyctionActivity,TF, sousuoBean.special_service_cat, object : PopShejiXiaoshouDuoxuan.duoxuan {
                    override fun xuanze(list: MutableList<String>, listid: MutableList<String>) {
                        val shejishi = StringBuffer("")
                        for (j in 0 until list.size) {
                            if (j == list.size - 1) {
                                shejishi.append(list[j])
                            } else {
                                shejishi.append(list[j])
                                shejishi.append(" ")
                            }
                        }
                        Myutil.settv(shejishi.toString(), tvTeFu)
                        mtefu = list
                    }

                }, "请选择销售员")
                pop.show(Sputil.sp("erp"), tvTeFu)
            }
            but_queding -> {
                getsearchparams()
                gettime()
                sousuoBean.special_service_cat=mtefu
                val intent = Intent()
                setResult(Activity.RESULT_OK, intent)
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
            tv_start_time -> {
                Myutil.notjianpan(this@ProdyctionActivity, iv_header_left)
                Dialogutil.showBottoPopupWindow(tv_start_time, "选择开始时间", tv_start_time.text.toString(), this@ProdyctionActivity)
            }
            tv_end_time -> {
                Myutil.notjianpan(this@ProdyctionActivity, iv_header_left)
                Dialogutil.showBottoPopupWindow(tv_end_time, "选择结束时间", tv_end_time!!.text.toString(), this@ProdyctionActivity)
            }
            iv_header_left -> {
                finish()
                overridePendingTransition(0, R.anim.end_activity1)
            }
        }
    }

    private fun startSeek(select: Int, requestCode: Int, title: String, data: String) {
        val i = intent.setClass(this@ProdyctionActivity, SeekActivity::class.java)
        i.putExtra("select", select)
        i.putExtra("data", data)
        startActivityForResult(intent, requestCode)
        overridePendingTransition(R.anim.start_activity1, R.anim.no_activity)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(data==null){
            return
        }
        when (requestCode) {
            7 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                tv_diangdanhao.text = s
            }
            8 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                tv_hetonghao.text = s
            }
            9 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                tv_kehuming.text = s
            }
            10 -> if (resultCode == Activity.RESULT_OK) {
                val s = data.getStringExtra("name")
                tv_zhuanmaidian.text = s
            }
        }
    }

    override fun onKeyDown(keyCode: Int, event: KeyEvent): Boolean {
        if (keyCode == 4) {
            overridePendingTransition(0, R.anim.end_activity1)
        }
        return super.onKeyDown(keyCode, event)
    }

    companion object {
        val TAG = "ProdyctionActivity"
        private val a = "ProdyctionActivity"
    }
}
